package com.ruix.app.ui.stratum;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.adapter.superadapter.IMulItemViewType;
import com.ruix.app.cache.CacheFactory;
import com.ruix.app.manager.UserManager;
import com.ruix.app.mapper.OrganizationListMapper;
import com.ruix.app.model.Classes;
import com.ruix.app.network.CacheObserver;
import com.ruix.app.network.RXObserver;
import com.ruix.app.network.RequestFactory;
import com.ruix.app.ui.adapter.ClassListAdapter;
import com.ruix.app.ui.base.BaseActivity;

import java.util.ArrayList;

import io.reactivex.disposables.Disposable;

/**
 * Created by hadexs on 2017/7/1.
 */

public class OrganizCircleListActivity extends BaseActivity implements  AdapterView.OnItemClickListener,SwipeRefreshLayout.OnRefreshListener {


    public static Intent newIntent(Context context, Classes classes){
        Intent intent = new Intent(context,OrganizCircleListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("classes",classes);
        intent.putExtras(bundle);
        return intent;
    }

    public Classes currentClasses;
    private ListView classesList;
    private ClassListAdapter adapter;
    private ArrayList clasessInList;//
    private SwipeRefreshLayout mRefreshLayout;
    private View noMessageTip;

    @Override
    protected int loadViewLayout() {
        return R.layout.activity_organiz_circle_list;
    }

    @Override
    protected void initView() {
        loadIntent();
        setTitle(currentClasses.getName());

        noMessageTip = findViewById(R.id.tv_no_data_tip);
        noMessageTip.setVisibility(View.GONE);
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
        mRefreshLayout.setColorSchemeResources(R.color.bg_nav_default
                , R.color.bg_nav_yellow
                , R.color.bg_default
                , R.color.text_color_pink);

        classesList = (ListView) findViewById(R.id.lv_classes);
        adapter = new ClassListAdapter(this, null, new IMulItemViewType<Classes>() {
            @Override
            public int getViewTypeCount() {
                return 1;
            }

            @Override
            public int getItemViewType(int position, Classes classes) {
                return 0;
            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_grade_list;
            }
        });
        classesList.setAdapter(adapter);
    }

    private void loadIntent(){
        Intent intent = getIntent();
        if (intent != null){
            currentClasses = (Classes)intent.getSerializableExtra("classes");
        }
    }

    @Override
    protected void initLocalData() {
        clasessInList = new ArrayList();
        loadCircleListByCache();
        loadClassesList(false);
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void initListener() {
        classesList.setOnItemClickListener(this);
        mRefreshLayout.setOnRefreshListener(this);
    }


    private void loadCircleListByCache(){
        CacheFactory.loadOrgnizationCircleList(currentClasses.getID(),new CacheObserver<ArrayList<Classes>>(){
            @Override
            public void onNext(ArrayList<Classes> value) {
                if (value != null && value.size() > 0){
                    clasessInList = value;
                    adapter.replaceAll(clasessInList);
                }
            }

        });
    }

    private void loadClassesList(final boolean refresh){
        if (!refresh){
            showLoading();
        }
        RequestFactory.organizationList(currentClasses.getID(), new OrganizationListMapper(), new RXObserver<ArrayList<Classes>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(ArrayList<Classes> value) {
                if (value != null && value.size() > 0){
                    hideNoCircleTip();
                    clasessInList = value;
                    adapter.replaceAll(clasessInList);
                    CacheFactory.cacheOrgnizationCircleList(currentClasses.getID(),value);
                } else {
                    showNoCircleTip();
                }
            }

            @Override
            public void onError(Throwable e) {
                resolveException(e);
                if (refresh){
                    mRefreshLayout.setRefreshing(false);
                } else {
                    dismissLoading();
                }
            }

            @Override
            public void onComplete() {
                if (refresh){
                    mRefreshLayout.setRefreshing(false);
                } else {
                    dismissLoading();
                }
            }
        });

    }


    private void showNoCircleTip() {
        noMessageTip.setVisibility(View.VISIBLE);
        classesList.setVisibility(View.INVISIBLE);
    }

    private void hideNoCircleTip() {
        noMessageTip.setVisibility(View.GONE);
        classesList.setVisibility(View.VISIBLE);
    }

    @Override
    public void onRefresh() {
        loadClassesList(true);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (this.clasessInList != null) {
            Classes classes = (Classes) clasessInList.get(position);
            UserManager.sharedInstance().setCurrentClass(classes);
            Intent intent = ClassesStratumActivity.newIntent(this, classes,null);
            startActivity(intent);
            if (classes.isHaseNewMessage()) {
                classes.setHaseNewMessage(false);
                adapter.notifyDataSetHasChanged();
            }
        }
    }
}
